import java.util.ArrayList;
import java.util.Collections;

/**
 * MasterDriver
 * This class runs xmlrpcmaster multiple times with several configurations of matrices & varying number of nodes 
 * @author PRP
 *
 */
public class MasterDriver {

	/**
	 * main- runner to get data to test the hypothesis.
	 * Runs master in various combinations determined from the input.
	 * Usage: java -jar xmlrpcmaster.jar <numtrials> <matrixsize,matrixsize...> <slavehostname:port,slavehostname:port,...>
	 * ex: java -jar xmlrpcmaster.jar 10 10,50,100,500,1000,1500,2000 localhost:8080,localhost:8081,localhost:8082,localhost:8083,localhost:8084,localhost:8085
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length<3){
			System.err.println("Usage: java -jar masterdriver.jar <numtrials> <matrixsize,matrixsize...> <slavehostname:port,slavehostname:port,...> ");
			return;
		}
		//read in input arguments
		int numTrials = Integer.parseInt(args[0]);
		String[] matrixSizes= args[1].split(",");
		String[] hostPorts =args[2].split(",");
		
		System.out.println("Matrix_size,Number_nodes,Min_time(ms)");
		//For different matrix sizes
		for (String msize:matrixSizes){
			for (int i=0;i<hostPorts.length;i++){
				ArrayList<Long> times = new ArrayList<Long>();
				String argList[]=new String[i+2];
				argList[0]=msize;
				for(int j=1;j<i+2;j++){
					argList[j] = hostPorts[j-1];
				}
				//get time, numtrials times
				for(int k=0;k<numTrials;k++){
					times.add((new XMLRPCMaster()).execute(argList,false));
				}
				//print out the minimum time for each combination of matrix size and number of nodes
				System.out.println(msize+" x " + msize+","+ (i+1) + "," +Collections.min(times));
				
				
			}
			
		}
		

	}

}
